//using PK.InAppPurchase;
//using PKGames.Store;
using UnityEngine;
using VoxelEngine;

namespace VoxelGame
{
	[CreateAssetMenu(menuName = "Voxel/WorldSelectionConfig")]
	public class WorldSelectionConfig : ScriptableObject
	{
		public enum WorldState
		{
			Free,
			Trial,
			Locked,
			Unlocked
		}

		[SerializeField]
		private string _levelName;

		[SerializeField]
		private ResourceAsset _iconTextureAsset;

		[SerializeField]
		private ResourceAsset _worldData;

		//[SerializeField]
		//private StoreProduct _storeProduct;

		private WorldData _voxelWorldData;

		[SerializeField]
		private bool _free;

		private Sprite _iconTexture;

		private const string StoredTrialStateKeyPrefix = "trial_";

		//public StoreProduct Product => _storeProduct;

		public ResourceAsset WorldData => _worldData;

		public WorldData VoxelWorldData
		{
			get
			{
				if (_voxelWorldData == null)
				{
					_voxelWorldData = _worldData.Asset<WorldData>();
				}
				return _voxelWorldData;
			}
			set
			{
				_voxelWorldData = value;
			}
		}

		public Sprite IconTexture
		{
			get
			{
				if (_iconTexture == null)
				{
                    Sprite sprite = _iconTextureAsset.Asset<Sprite>();
                    //Debug.Log(sprite);
					_iconTexture = _iconTextureAsset.Asset<Sprite>();
				}
				return _iconTexture;
			}
		}

		private string StoredTrialStateKey => "trial_" + _levelName;

		public bool TrialEnabled
		{
			get
			{
				return PlayerPrefs.GetInt(StoredTrialStateKey, 1) == 1;
			}
			set
			{
				PlayerPrefs.SetInt(StoredTrialStateKey, value ? 1 : 0);
				PlayerPrefs.Save();
			}
		}

		public WorldState CurrentWorldState
		{
			get
			{
				if (_free)
				{
					return WorldState.Free;
				}
				//if (StoreManager.Instance.IsProductBought(Product))
				//{
				//	return WorldState.Unlocked;
				//}
				return TrialEnabled ? WorldState.Trial : WorldState.Locked;
			}
		}

		public void Dispose()
		{
			_iconTexture = null;
		}
	}
}
